package com.jpyl.jpMedicalTreatment.view.activity.self;

import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.forxindu.kit.box.ViewKit;
import com.jpyl.jpMedicalTreatment.R;
import com.jpyl.jpMedicalTreatment.model.ResponseModel;
import com.jpyl.jpMedicalTreatment.model.RxSpf_UserModel;
import com.jpyl.jpMedicalTreatment.network.Network;
import com.jpyl.jpMedicalTreatment.network.api.ApiError;
import com.jpyl.jpMedicalTreatment.network.api.RequestApi;
import com.jpyl.jpMedicalTreatment.util.StringUtil;
import com.jpyl.jpMedicalTreatment.view.activity.BaseActivity;
import com.jpyl.jpMedicalTreatment.view.activity.TestActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * 反馈、申诉、举报界面
 * Created by 15697 on 2019/1/7.
 */

public class FeedbackActivity extends BaseActivity {


    @BindView(R.id.et_feedBack_content)
    EditText etFeedBackContent;
    @BindView(R.id.tv_feedBack_length)
    TextView tvFeedBackLength;
    @BindView(R.id.btn_feedBack_submit)
    Button btnFeedBackSubmit;
    RxSpf_UserModel spfUserModel;
    final RequestApi publicApi = Network.getRequestApi();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_feed_back;
    }

    @Override
    public void afterCreate(Bundle savedInstanceState) {
        mTitleTv.setText(R.string.mine_feedBack);
        StringUtil.setEtLength(tvFeedBackLength, etFeedBackContent);
        spfUserModel = RxSpf_UserModel.create(this);
    }

    @OnClick(R.id.btn_feedBack_submit)

    public void onClick() {
        String userid = spfUserModel.userId().get();
        String content = etFeedBackContent.getText().toString();
        if (StringUtil.isEmpty(content)) {
            etFeedBackContent.requestFocus();
            ViewKit.showToast(FeedbackActivity.this, "请输入反馈");
            return;
        }
        publicApi.jpylComplain(userid, content).map(new Func1<ResponseModel<String>,
                String>() {
            @Override
            public String call(ResponseModel<String> responseModel) {
                if (responseModel.getCode() != 0) {
                    String msg = responseModel.getMsg();
                    throw new ApiError(responseModel.getCode(), msg);
                }
                return responseModel.getMsg();
            }
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                        if (e instanceof ApiError) {
                            ApiError error = (ApiError) e;
                            ViewKit.showToast(FeedbackActivity.this, error.getMsg() + "");
                        }
                    }

                    @Override
                    public void onNext(String msg) {
                        ViewKit.showToast(FeedbackActivity.this, msg + "");
                        finish();
                    }
                });

    }

}
